using System;
using System.Collections.Generic;
using System.Linq;
using FishFillets.Model;
using FishFillets.Model.GameItems;
using Microsoft.Xna.Framework;

namespace FishFillets.GameComponents.Helpers
{
    /// <summary>
    /// 
    /// </summary>
    public static class SelectHelper
    {
        public static Fish TrySelectFish(Level level, int x, int y)
        {
            int xpos = x/Common.BlockWidth;
            int ypos = y/Common.BlockHeight;
            if (GetObjetcPoints(level.Big, 0, 0).Contains(new Point(xpos, ypos)))
            {
                return level.Big;
            }
            if (GetObjetcPoints(level.Small, 0, 0).Contains(new Point(xpos, ypos)))
            {
                return level.Small;
            }
            return null;
        }

        public static LevelRef TrySelectLevelRef(Map map, int x, int y)
        {
            // TODO check distance from center, not rectangle around it
            return map.Levels.FirstOrDefault(level => 
                x > level.Position.X - 10 &&
                y > level.Position.Y - 10 &&
                x < level.Position.X + 10 &&
                y < level.Position.Y + 10);
        }

        private static List<Point> GetCollisionPoints(BaseItem obj, Direction? dir)
        {
            if (dir == null)
            {
                return GetObjetcPoints(obj, 0, 0);
            }
            switch (dir.Value)
            {
                case Direction.Up:
                    return GetObjetcPoints(obj, 0, -1);
                case Direction.Left:
                    return GetObjetcPoints(obj, -1, 0);
                case Direction.Down:
                    return GetObjetcPoints(obj, 0, 1);
                case Direction.Right:
                    return GetObjetcPoints(obj, 1, 0);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private static List<Point> GetObjetcPoints(BaseItem obj, int xd, int yd)
        {
            List<Point> list = new List<Point>();
            for (int y = 0; y < obj.ItemMap.GetLength(1); y++)
            {
                for (int x = 0; x < obj.ItemMap.GetLength(0); x++)
                {
                    if (obj.ItemMap[x, y])
                    {
                        list.Add(new Point(obj.Position.X + x + xd, obj.Position.Y + y + yd));
                    }
                }
            }
            return list;
        }
    }
}
